package edu.unc.comp410.colemangeer.model.constraints;
import static java.lang.System.err;
import static java.lang.System.out;


import org.apache.log4j.Logger;

import edu.unc.comp410.colemangeer.data.ConstraintGatewayEmulator;
import edu.unc.comp410.colemangeer.data.IConstraintGateway;
import edu.unc.comp410.colemangeer.data.IntegerConstraintGatewayResult;


/**
 * An <CODE>Integer</CODE> field constrained to a contiguous
 * range of values: getMinValue() <= value <= getMaxValue()
 */
public class IntegerConstraint extends ObjectConstraint {
	
		// Constants
		// Default DB and machine.
//	private static final String DB = "customer1";
//	private static final String MACH = "mydb";
	
		// Data members
	private Integer minValue;	// Range of values allowed.
	private Integer maxValue;
	private static IConstraintGateway constraintGateway;
	private static Logger log = Logger.getLogger( IntegerConstraint.class );
	
	static {	// Set default constraint gateway.
//		try {	// If possible, establish a connection to the DB.
//				// If not, use a stub.
//			constrGateway 
//				= new ConstraintGateway( DB, MACH );
//		} catch (ClassNotFoundException e) {
//			constrGateway = new ConstraintGatewayEmulator( );
//		}
//		if( ! constrGateway.successfulDBconnection())
		
		System.out.println("Static block in IntegerConstraint....");
		constraintGateway = new ConstraintGatewayEmulator( );
		log.warn( constraintGateway );
	}
	
	public IntegerConstraint( String theName, boolean theOptional,
			Integer theMin, Integer theMax ) {
		super( theName, theOptional, Integer.class );
		minValue = theMin;
		maxValue = theMax;
	}
	
	public IntegerConstraint( String theName, boolean theOptional ) {
		super( theName, theOptional, Integer.class );
		
		final IntegerConstraintGatewayResult res 
			= constraintGateway.findIntegerConstraint( theName );
		if( res.success ) {
			minValue = res.Min;
			maxValue = res.Max;
		}
		else throw new RuntimeException 
				( "DB could not find constraint " + theName );
		log.debug( " Constructor: " + this.toString() );
	}

	public Integer getMinValue() { return minValue; }
	public Integer getMaxValue() { return maxValue; }

	@Override public boolean 
	isLegal( Object theObject ) {
		if( ! super.isLegal( theObject ) ) return false;
		if( null == theObject ) return true;
		boolean result = true;
		String msg = "IntegerConstraint: ";
		Integer theValue = (Integer) theObject;
		if( null != theValue ) {
			if( theValue < minValue || theValue > maxValue ) {
				result = false;
				msg += "value " + theValue + " out of range " 
				+ getMinValue() + " ... " + getMaxValue();
			}
		}
//		if( ! result ) err.println( "IntegerConstraint.legal: msg = "
//			+ msg );
		return result;
	}
	
	/**
	 * Used only for testing.
	 * @param newCGI
	 */
	public static void 
	setConstraintGateway( IConstraintGateway newCGI ) {
		constraintGateway = newCGI;
	}
	
	@Override public String 
	toString() {
		String result = "Integer Constraint = " + getConstraintName() +
			"; Min = " + getMinValue() + " Max = " 
			+ getMaxValue();
		return result;
	}

}

